package com.bj58.groupbuy.action.inter;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.groupbuy.util.PathUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

/**从logData(mongo)中抽取联盟数据中的流量数据
 * @author sunny3791
 *
 */
public class AllianceFlux {
	private static DBCollection coll;
	private static Mongo m;
	private static DB  db;
	
	
	private DBCollection getConnection() throws Exception {
		String serviceIp = "10.48.176.170";
		String databaseName="hadoop_topinfo";
		String mapName="addisp";
		int port = 27017;
		
		m = new Mongo(serviceIp,port);  
        db = m.getDB(databaseName);  
        coll = db.getCollection(mapName);
		
        System.out.println("CONNECTION TO:"+serviceIp+"/"+databaseName+"/"+mapName);
        System.out.println(coll);
        return coll;
	}
	
	
	private String getData(String searchDate)throws Exception{
		StringBuffer  rs = new StringBuffer();
		coll= getConnection();
        BasicDBObject obj = new BasicDBObject();
        obj.put("_id", searchDate);
        DBCursor cursor = coll.find(obj);
        while (cursor.hasNext()) {
        	DBObject  data = cursor.next();
        	System.out.println(data.toString());
        	String date = data.get("_id").toString();
        	String baidu = data.get("baidu").toString();
        	
        	String baidu_afc = "0";
        	if(data.containsField("baidu_afc")){
        		baidu_afc = data.get("baidu_afc").toString();
        	}
        	
        	String baidu_afs = data.get("baidu_afs").toString();
        	
        	String google = "0";
        	if(data.containsField("google")){
        		google = data.get("google").toString();
        	}
        	
        	
        	String google_afc = "0";
        	if(data.containsField("google_afc")){
        		google_afc = data.get("google_afc").toString();
        	}
        	
        	
        	String google_afs = "0";
        	if(data.containsField("google_afs")){
        		google_afs = data.get("google_afs").toString();
        	}
        	
        	String list  = data.get("list").toString();
        	String post = data.get("post").toString();
        	
        	String sogou = "";
        	if(data.containsField("sogou")){
        		sogou = data.get("sogou").toString();
        	}
        	
        	
        	int total = Integer.parseInt(list)+ Integer.parseInt(post);
        	
        	rs.append(date+"&");
        	rs.append(baidu+"&");
        	
        	rs.append(baidu_afc+"&");
        	
        	rs.append(baidu_afs+"&");
        	rs.append(google+"&");
        	rs.append(google_afc+"&");
        	rs.append(google_afs+"&");
        	rs.append(list+"&");
        	rs.append(post+"&");
        	rs.append(sogou+"&");
        	rs.append(total);
        }
        cursor.close();
        System.out.println(rs.toString());
		return rs.toString();
	}
	
	
	
	  public static void main(String[] args) {
	    	try {
	    		AllianceFlux  orf = new AllianceFlux();
	        	CommonDao  comDao = new CommonDao();
	        	
//	        	String startDate = "2014-02-13";
//	        	String endDate = "2014-02-13";
	    		String startDate = DateUtil.getStringDate(DateUtil.getDateBefore(new Date(), 1), "yyyy-MM-dd");
				String endDate = DateUtil.getStringDate(DateUtil.getDateBefore(new Date(), 1), "yyyy-MM-dd");
	        	
				if(args.length > 0  && args.length <= 2){
		    		if(args.length  == 1){
		    			startDate = args[0].trim();
		    			endDate  =  args[0].trim();
		    		}else{
		    			startDate = args[0].trim();
		    			endDate  =  args[1].trim();
		    		}
				}
				System.out.println(startDate);
		    	System.out.println(endDate);
				
				String deleteCon = " and statDate >= '"+startDate+"' and statDate <='"+endDate+"' ";
				comDao.deleteData("flux_all_day", deleteCon, DBConfig.db_68_ors.getIntType());
	        	
	        	List<String>  dateList = DateUtil.getDateList(startDate, endDate);
	        	StringBuffer result = new StringBuffer();
				for(String searchDate : dateList){
					String data = orf.getData(searchDate);
					System.out.println(searchDate);
					if("".equals(data)){
						continue;
					}
					result.append(data+"\r\n");
				}
				//写文件
				String filePath = PathUtil.getWFNamespaceFolder()+"/dbrecord/OnlineRevenueFlux_"+startDate+".txt";
				File file = new File(filePath);
				if(file.exists()){
					file.delete();
				}
				RandomAccessFile raf = new RandomAccessFile(file, "rw");
				raf.seek(raf.length());
				raf.write(result.toString().getBytes("UTF-8"));	
				raf.close();
				//load
			  	System.out.println("Loading....");
	        	comDao.loadData(filePath, "flux_all_day", "&", DBConfig.db_68_ors.getIntType());
				
	        	System.out.println("done~~!"); 
			} catch (Exception e) {
				SomePublicUtil.emailWarn("AllianceFlux has error~");
				e.printStackTrace();
			}finally{
				System.exit(0);
			}
	    }
}
